import { login, isFrozen } from "../../server/api";
import { debounce } from "../../server/shake";
Page({
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   // 在小程序启动时检测微信版本
wx.getSystemInfo({
  success: function(res) {
    const version = res.SDKVersion;
    // 判断版本是否支持TabBar图标
    if (compareVersion(version, '3.0.2') < 0) {
      // 版本过低，显示提示给用户
      wx.showModal({
        title: '微信版本过低',
        content: '您的微信版本过低，部分功能可能无法正常显示，请升级到最新版本。',
        showCancel: false
      });
    }
  }
});

// 比较版本号的函数
function compareVersion(v1, v2) {
  v1 = v1.split('.');
  v2 = v2.split('.');
  const len = Math.max(v1.length, v2.length);

  while (v1.length < len) {
    v1.push('0');
  }
  while (v2.length < len) {
    v2.push('0');
  }

  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i]);
    const num2 = parseInt(v2[i]);

    if (num1 > num2) {
      return 1;
    } else if (num1 < num2) {
      return -1;
    }
  }

  return 0;
}
  },
  getPhoneNumber: debounce(async function (e) {
    console.log(e,'e');
    const detail = e.detail;

    if (detail.errMsg == "getPhoneNumber:ok") {
      var phone_code = detail.code;
      wx.login({
        success: async (res) => {
          const params = {
            js_code: res.code,
            phone_code: phone_code,
          };
          const data = await login(params);
          console.log(data, "data");
          if (data.code == 200) {
            const value = data.result;
            wx.setStorageSync("token", value.token);
            wx.setStorageSync("userInfo", value.userInfo);
            wx.setStorageSync("phone", value.userInfo.phone);
            wx.setStorageSync("id", value.userInfo.id);
            wx.setStorageSync("openId", value.userInfo.openId);
            wx.setStorageSync("userType", value.userInfo.userType);
            const newuser = value.userInfo.isNewUser;
            const usertype = value.userInfo.userType;
            const val = await isFrozen();
            if (val.result == "false") {
              //isNewUser  判断是否是新用户(true)
              if (newuser == true) {
                //跳转到归属地
                wx.redirectTo({
                  url: "/subPackagesA/pages/Mobilephone/Mobilephone",
                });
              } else {
                //type 1  跳转到用户端首页
                if (usertype == 1) {
                  wx.switchTab({
                    url: "/pages/pointsmall/pointsmall",
                  });
                } else if (usertype == 2) {
                  wx.switchTab({
                    url: "/pages/merchanthome/merchanthome",
                  });
                } else {
                  wx.redirectTo({
                    url: "/pages/management/management",
                  });
                }
              }
            } else {
              wx.showToast({
                title: "您的账号已被冻结，请联系管理员",
                icon: "none",
              });
            }
          }
        },
      });
    }
  },3000),

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
